import 'package:flutter/cupertino.dart';
import 'package:rxdart/rxdart.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/routers.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class PersonalHomePageViewModel extends BaseViewModel {
  PersonalInfoResponse? _personalInfo;
  PublishSubject loadingState = PublishSubject<CommonLoadingState?>();

  PersonalInfoResponse? getPersonalInfoResponse() => _personalInfo;

  String getUserSex() {
    String sex = '';
    switch (_personalInfo?.sex) {
      case 0:
        sex = '女';
        break;
      case 1:
        sex = '男';
        break;
      default:
        sex = '保密';
        break;
    }
    return sex;
  }

  String getIconAddress() {
    return UrlContract.baseAddress + '/' + (_personalInfo?.icon?.iconSrc ?? '');
  }

  // 跳转到编辑个人信息界面
  Future<void> turnToEditPersonalInfoPage(BuildContext context) async {
    await getApplication().routerNavigateTo(
      context,
      RoutersPath.editPersonalInfoPage,
    );
  }

  Future<void> turnToPersonalFriendCirclePage(BuildContext context) async {
    await getApplication().routerNavigateTo(
      context,
      RoutersPath.editPersonalFriendCircleListPage,
    );
  }

  // 拉取个人信息
  Future<void> pullPersonalInfo({
    OnFailure? onFailure,
  }) async {
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    Map<String, dynamic>? json = await getNetworkUtil().postHttp(
      UrlContract.pullPersonalInfo,
      params: params,
    );
    _personalInfo = PersonalInfoResponse.fromMap(json);
    if (_personalInfo?.status == 0) {
      loadingState.add(CommonLoadingState.successTypeOne);
    } else {
      loadingState.add(CommonLoadingState.failure);
      if (onFailure != null) {
        final commonResp = CommonResponse.fromMap(json);
        onFailure(commonResp.msg);
      }
    }
  }

  // 跳转到密码修改页面
  Future<void> turnToPasswordChangePage(BuildContext context) async {
    getApplication()
        .getRouter()
        ?.navigateTo(context, RoutersPath.passwordChangePage);
  }

  // 跳转到教师认证界面
  Future<void> turnToTeacherCertificationPage(BuildContext context) async {}

  // 跳转到隐私页面
  Future<void> turnToPrivacyPage(BuildContext context) async {
    getApplication().routerNavigateTo(context, RoutersPath.privacyPage);
  }

  // 跳转到软件设置界面
  Future<void> turnToSettingsPage(BuildContext context) async {
    getApplication().routerNavigateTo(context, RoutersPath.settingsPage);
  }
}
